package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.ALiSMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){

        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        System.out.println("验证码："+code);
        //调用短信服务商发送验证码
        //ALiSMSUtils.sendCodeMessage(telephone,code);
        //短信发送成功，保存到redis(对应业务+telephone作为验证码的key)
        jedisPool.getResource().setex(RedisMessageConstant.SENDTYPE_ORDER+telephone,60*60,code.toString());
        return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);


    }

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        System.out.println(code);
        jedisPool.getResource().setex(RedisMessageConstant.SENDTYPE_LOGIN+telephone,60*60,code.toString());
        return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

}
